package com.bayou4j.sms;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

//import common.util.web.Base;

public class SendSMS {

	private String encode = "GB2312";

	public String getEncode() {
		return encode;
	}

	public void setEncode(String encode) {
		this.encode = encode;
	}

	private String servicesRequestAddRess = "";

	private String username = "";

	private String password = "";

	private int smstype = 1;

	private int timerflag = 0;

	private String timervalue;

	private int timertype = 1;

	private String timerid;

	private String mobiles;

	private String message;

	public String getServicesRequestAddRess() {
		return servicesRequestAddRess;
	}

	public void setServicesRequestAddRess(String servicesRequestAddRess) {
		this.servicesRequestAddRess = servicesRequestAddRess; // Base.base64Decode(servicesRequestAddRess);
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getSmstype() {
		return smstype;
	}

	public void setSmstype(int smstype) {
		this.smstype = smstype;
	}

	public int getTimerflag() {
		return timerflag;
	}

	public void setTimerflag(int timerflag) {
		this.timerflag = timerflag;
	}

	public String getTimervalue() {
		return timervalue;
	}

	public void setTimervalue(String timervalue)

	{
		this.timervalue = timervalue;
	}

	public int getTimertype() {
		return timertype;
	}

	public void setTimertype(int timertype) {
		this.timertype = timertype;
	}

	public String getTimerid() {
		return timerid;
	}

	public void setTimerid(String timerid) {
		this.timerid = timerid;// Base.base64Decode(timerid);
	}

	public String getMobiles() {
		return mobiles;
	}

	public void setMobiles(String mobiles) {
		this.mobiles = mobiles;// Base.base64Decode(mobiles);
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		try {
			this.message =

			URLEncoder.encode(message, this.getEncode());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public Map<String, String> sendSMS() {
		DocumentBuilderFactory dbf =

		DocumentBuilderFactory.newInstance();
		dbf.setNamespaceAware(true);
		Document doc;
		Map<String, String> result = new

		LinkedHashMap<String, String>();
		try {
			DocumentBuilder db =

			dbf.newDocumentBuilder();
			InputStream is =

			URLConnIO.getSoapInputStream(this

			.getServicesRequestAddRess(), this.getRequestData

			().toString());
			doc = db.parse(is);
			result.put("errorcode",

			doc.getElementsByTagName("errorcode").item(

			0).getFirstChild().getNodeValue());
			result.put

			("errordescription", doc.getElementsByTagName(

			"errordescription").item(0).getFirstChild

			().getNodeValue());
			result.put("time",

			doc.getElementsByTagName("time").item(0)

			.getFirstChild().getNodeValue());
			result.put("msgcount",

			doc.getElementsByTagName("msgcount").item(0)

			.getFirstChild().getNodeValue());
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("���Ͷ���ʧ��!");
		}
		return result;
	}

	private StringBuffer getRequestData() {
		StringBuffer requestAddRess = new

		StringBuffer();
		requestAddRess.append

		("func=sendsms&username=");
		requestAddRess.append

		(this.username);
		requestAddRess.append("&password=");
		requestAddRess.append(password);
		requestAddRess.append("&smstype=");
		requestAddRess.append(smstype);
		requestAddRess.append

		("&timerflag=");
		requestAddRess.append

		(this.timerflag);
		if (this.timerflag != 0) {
			requestAddRess.append

			("&timervalue=");
			requestAddRess.append

			(this.timervalue);
		}
		requestAddRess.append

		("&timertype=");
		requestAddRess.append

		(this.timertype);
		requestAddRess.append("&timerid=");
		requestAddRess.append(this.timerid);
		requestAddRess.append("&mobiles=");
		requestAddRess.append(this.mobiles);
		requestAddRess.append("&message=");
		requestAddRess.append(this.message);
		return requestAddRess;
	}
}
